﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public static class AnimatorExtensions
    {
        public static IEnumerator WaitEvent(this Animator animator,string eventname)
        {
            AnimatorEventDispatcher dispatcher = animator.GetOrAddComponentDontSave<AnimatorEventDispatcher>();
            return dispatcher.WaitEvent(eventname);
        }

        public static void WaitEvent(this Animator animator, string eventname, Action<string, AnimatorStateInfo> complete)
        {
            AnimatorEventDispatcher dispatcher = animator.GetOrAddComponentDontSave<AnimatorEventDispatcher>();
            dispatcher.WaitEvent(eventname, complete);
        }

        public static SignalHandle ListenEvent(this Animator animator, string eventName, Action<string, AnimatorStateInfo> eventDelegate)
        {
            AnimatorEventDispatcher dispatcher = animator.GetOrAddComponentDontSave<AnimatorEventDispatcher>();
            return dispatcher.ListenEvent(eventName, eventDelegate);
        }

        public static AnimationClip GetAnimationClip(this Animator animator, string name)
        {
            RuntimeAnimatorController runtimeAnimatorController = animator.runtimeAnimatorController;
            if (runtimeAnimatorController == null)
            {
                return null;
            }
            AnimatorOverrideController animatorOverrideController = runtimeAnimatorController as AnimatorOverrideController;
            if (animatorOverrideController != null)
            {
                return animatorOverrideController[name];
            }
            foreach (AnimationClip animationClip in runtimeAnimatorController.animationClips)
            {
                if (animationClip.name == name)
                {
                    return animationClip;
                }
            }
            return null;
        }

    }
}

